/**
 * @module 战争模块
 */
import {creatCreep} from "../config/creepNum/creepNumCore.js";
import {DEFENDER, ENDER} from "../constant/roleConstant.js";
import {judge} from "./creepNumController.js";

export const warController = (roomKey) => {
    //监测房间中是否存在侵略者
    if (Game.rooms[roomKey].find(FIND_HOSTILE_CREEPS).length > 0) {
        //抵御者为0后，启动安全模式

        const spawns = Memory.Rooms[roomKey].spawns.map((id) => Game.getObjectById(id))
        for (const spawn of spawns) {
            if (judge(DEFENDER, roomKey) && !spawn.spawning) {
                creatCreep(spawn, roomKey, DEFENDER)
            }
        }
        //容错机制
        //todo 修改容灾机制
        if (Game.rooms[roomKey].find(FIND_MY_CREEPS).length === 0) {
            for (const spawn of spawns) {
                if (judge(ENDER, roomKey) && !spawn.spawning) {
                    creatCreep(spawn, roomKey, ENDER)
                }
            }
        }
        // Game.rooms[roomKey].controller.activateSafeMode()
    }

}